package com.ulook.lookplanet.ui.search;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;


import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.ui.square.VArticlePreInfo;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

public class SearchViewModel extends BaseViewModel2 {

    private int currUserId;
    private MutableLiveData<List<VArticlePreInfo>> loadData = new MutableLiveData<>();

    public int getCurrUserId() {
        return currUserId;
    }

    public void setCurrUserId(int currUserId) {
        this.currUserId = currUserId;
    }

    public LiveData<List<VArticlePreInfo>> getLoadData(){
        return loadData;
    }

    /**
     * 搜索动态
     */
    public void loadData(String s, int itemNum){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<VArticlePreInfo> articlePreInfos = JacksonUtil.json2list((String)res, VArticlePreInfo.class);
                    loadData.setValue(articlePreInfos);
                } catch (Exception e) {
                    setToastText("获取动态解析数据失败");
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText(failuer.getEmsg());
                ThrowException.print(failuer, failuer.getEmsg());
            }
        };
        RequestParams params = new RequestParams();
        params.put("s", s);
        params.put("currUserId", currUserId);
        params.put("itemNum", itemNum);
        AsynRequestUtil.post(AppServer.ROOT_PATH + "/article/searchArt", params, null, callback);
    }

}